/**
 * 
 */
package com.icbc.cc.api;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.alibaba.fastjson.JSONObject;
import com.icbc.cte.base.CTEConstance;
import com.icbc.cte.base.TranFailException;
import com.icbc.ctp.core.IContext;
import com.icbc.ctp.core.annotations.Input;
import com.icbc.ctp.core.impl.OperationStep;


/**
 * TODO 类的描述：。 
 * <pre>
 *
 * </pre>
 *
 * <pre>
 * modify by user on 2016-11-7
 *    fix->1.
 *         2.
 * </pre> 
 */
@Input(name = "httpPath", type = "java:java.lang.String", mustInput = true, isVariable = true, isArray = false, description = "远程文件路径")
public class RemoteFileMgmtOp extends OperationStep {

	/* (non-Javadoc)
	 * @see com.icbc.ctp.core.impl.OperationStep#execute(com.icbc.ctp.core.IContext)
	 */
	@Override
	protected int execute(IContext arg0) throws TranFailException {
		// TODO Auto-generated method stub
		String httpPath=(String) arg0.getValueAt("httpPath");
		String newhttpPath=httpPath.replaceAll("uploadFiles", "uploadfiles");
		String fileServerPath=(String) arg0.getValueAt("fileServerPath");
		
		String localPath=CTEConstance.getEnvProperty("file_rl_path");
		if(!new FileUtil().saveFileToLocale(fileServerPath+httpPath , localPath+newhttpPath , true)){
			arg0.setValueAt("retCode", "4");
			return 0;
		}
		String fileId = (String) arg0.getValueAt("fileId");
		String fileType=(String) arg0.getValueAt("fileType");
		String sourceId=(String) arg0.getValueAt("sourceId");
		String productType=(String) arg0.getValueAt("productType");
		try {
			JSONObject newjson=new JSONObject();
			//产品编号
			newjson.put("fileId", fileId);
			newjson.put("isdownload", "1");
			newjson.put("filePath", localPath+httpPath.replaceAll("uploadFiles", "uploadfiles"));
			newjson.put("httpPath", httpPath.replaceAll("uploadFiles", "uploadfiles"));
			newjson.put("fileType", fileType);
			newjson.put("sourceId", sourceId);
			newjson.put("productType", productType);
			Date date=new Date();
			DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			newjson.put("saveDate", format.format(date));
			
			String fileResult = ApiCommonUtils.excuteOp(newjson.toJSONString(), "insert_fileinfo");
			JSONObject fileobj=JSONObject.parseObject(fileResult);
			String fileCode = fileobj.getString("retCode");
			if("0".equals(fileCode)){
				arg0.setValueAt("retCode", "0");
				arg0.setValueAt("retMsg", "文件存储成功");
			}else{
				arg0.setValueAt("retCode", "-1");
				arg0.setValueAt("retMsg", "文件存储失败");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		arg0.setValueAt("retCode", "0");
		return 0;
	}

}
